Ext.define('Lms.rms.AdminMgmPanel',{
	extend : 'Ext.grid.Panel',
	initComponent : function(){
		Ext.define('AdminModel', {
	        extend: 'Ext.data.Model',	        
	        fields: [
	            'id', 'name', 'pw', 'email', 'auth'
	        ],
	        idProperty: 'id'
	    });
		
		var store = Ext.create('Ext.data.Store',{
			model : 'AdminModel',
			sortInfo: {
		        field    : 'name',
		        direction: 'ASC'
		    },
			proxy : {
				type : 'ajax',
				url : 'admin/pageList.do',
				actionMethods : {
					read : 'POST'					
				},
				reader : {
					type : 'json',
					root: 'list',
	                totalProperty: 'totalCount'
				}
			}
		});
		
		Ext.apply(this,{
			id : 'AdminMgmPanel',
			title : LocaleUtil.getMessage('rms.menu.adminMng'),
			store : store,
			selType: 'checkboxmodel',
			columns : this.getColumns(),
			viewConfig : Common.viewConfig(false),
			columnLines: true,
			tbar : ['->',{
				xtype : 'button',
				text : LocaleUtil.getMessage('reg'),
				iconCls : 'add',
				hidden : (loginAuth == 'S')?false:true,
				handler : function(){
					Ext.create('Lms.rms.AdminInfoDialog').openWindow();
				}
			},{
				xtype : 'button',
				text : LocaleUtil.getMessage('del'),
				iconCls : 'delete',
				hidden : (loginAuth == 'S')?false:true,
				scope : this,
				handler : this.onDeleteHandler
			}],
			bbar: Common.pagingToolBar(store)
		});
		this.callParent(arguments);
		this.on('celldblclick',this.onCellClickHandler);
		this.on('show',function(){
			store.load();
		},this);
	},
	
	getColumns : function(){
		return [
		    {text : LocaleUtil.getMessage('rms.label.adminId'),dataIndex : 'id', width:150},
		    {text : LocaleUtil.getMessage('rms.label.password'),dataIndex : 'pw'},
		    {text : LocaleUtil.getMessage('rms.label.name'),dataIndex : 'name', width:100},
		    {text : LocaleUtil.getMessage('rms.label.permissions'),dataIndex : 'auth'},
		    {text : LocaleUtil.getMessage('rms.label.email'),dataIndex : 'email', flex :1}
	    ];
	},
	
	onCellClickHandler : function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts){
		if(cellIndex == 0) return true;
		if(cellIndex == 1){
			Ext.create('Lms.rms.AdminInfoDialog').openWindow({id : record.get('id')});
		}
	},
	
	onDeleteHandler : function(){
		var selectedRecords = this.getSelectionModel().getSelection();
		if(selectedRecords.length == 0) return;
		
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToDeleteSelectedAdmin'),function(btn){
			if(btn == 'yes'){
				var ids = [];
				for ( var i = 0; i < selectedRecords.length; i++) {
					ids[i] = selectedRecords[i].get('id'); 
				}
				Ext.Ajax.request({
		            url:'admin/delete.do',
		            params: {
		            	ids : ids
		            },
		            scope : this,
		            success : function(result, request){
		                this.getStore().reload();
		            },
		            failure: Common.failureMessage
		        });  
			}
		},this);
	}
});